package top.pengdong.pictureShare.tweet.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import top.pengdong.pictureShare.common.model.PageUtils;
import top.pengdong.pictureShare.common.model.Query;
import top.pengdong.pictureShare.tweet.dao.PsTweetDao;
import top.pengdong.pictureShare.tweet.dao.PsTweetLikeForwardDao;
import top.pengdong.pictureShare.tweet.entity.PsTweetEntity;
import top.pengdong.pictureShare.tweet.entity.PsTweetLikeForwardEntity;
import top.pengdong.pictureShare.tweet.service.PsTweetLikeForwardService;
import top.pengdong.pictureShare.tweet.service.PsTweetService;


@Service("psTweetLikeForwardService")
public class PsTweetLikeForwardServiceImpl extends ServiceImpl<PsTweetLikeForwardDao, PsTweetLikeForwardEntity> implements PsTweetLikeForwardService {


    @Autowired
    private PsTweetDao psTweetDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PsTweetLikeForwardEntity> page = this.page(
                new Query<PsTweetLikeForwardEntity>().getPage(params),
                new QueryWrapper<PsTweetLikeForwardEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public PageUtils getUserInfoTweets(Long uid, Integer type, Integer currPage, Integer limit) {

        if (type == 3) {
            // TODO 进行浏览记录
        }

        IPage<PsTweetLikeForwardEntity> page = this.page(
                new Page<>(currPage, limit),
                new QueryWrapper<PsTweetLikeForwardEntity>()
                        .eq("user_id", uid)
                        .eq("type", type)
        );
        List<PsTweetLikeForwardEntity> records = page.getRecords();
        List<PsTweetEntity> list = new ArrayList<>();
        for (PsTweetLikeForwardEntity psTweetLikeForward : records) {
            PsTweetEntity psTweet = psTweetDao.selectById(psTweetLikeForward.getTweetId());
            if (psTweet != null)
                list.add(psTweet);
        }
        return new PageUtils(list, (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
    }
}